//
// Created by HP on 2024/2/6.
//

#ifndef AURORA_ST_AURORA_FOC_H
#define AURORA_ST_AURORA_FOC_H

#include "common_inc.h"
#include "as5047_driver.h"
#include "aurora_pid.h"
class Aurora_FOC{

#define SQRT_3 1.732050807568877f
#define SQRT_3DIV2 0.8660254037844386f

#define Uq_Limit_Max 6
#define Uq_Limit_Min -6

#define Ud_Limit_Max 6
#define Ud_Limit_Min -6

public:
    typedef enum Aurora_SEND_FOC_TYPE{
            SEND_NONE = 0,
            SEND_FOC_TaTbTc,
            SEND_FOC_IaIbIc,
            SEND_FOC_IqId,
            SEND_FOC_IalphaIbeta,
            SEND_FOC_Position,
    }Aurora_SEND_FOC_TYPE;

public:
    Aurora_FOC();


    void set_adc(ADC_HandleTypeDef *hadcx);
    void set_tim(TIM_HandleTypeDef *htimx,uint32_t channelu,uint32_t channelv,uint32_t channelw,uint32_t channelInJ = 0x0000000CU);
    void set_endocer(AS5047_Driver *driver);

    void set_mechanical_offset(float offset);
    void set_UqUd(float _Uq,float _Ud);

    void set_Wave_TaTbTc_MAXMIN(float max,float min);
    void set_Wave_IaIbIc_MAXMIN(float max,float min);
    void set_Wave_IqId_MAXMIN(float max,float min);
    void set_Wave_IalphaIbeta_MAXMIN(float max,float min);
public:
    void ctrl_halt_Loop();
    void ctrl_open_Loop();
    void ctrl_I_Loop();
    void ctrl_speed_Loop();
    void ctrl_position_Loop();

    void update_I(uint8_t valid_num,uint32_t Uu=0,uint32_t Uv=0,uint32_t Uw=0);

    void set_target_I(float I_q,float I_d);


    void calibration_angle_pole_pair();


    void Set_Iq_PI(float P,float I);
    void Set_Id_PI(float P,float I);

public:
    void set_send_wave_type(Aurora_SEND_FOC_TYPE wave_type);
public:
    void get_angle();
    void cal_angle_sincos();
    void clark();
    void iclark();
    void park();
    void ipark();
    void generate_svpwm();

    void update_pwm(float tu,float tv,float tw);
    void halt_pwm();
    void start_pwm();
    void start_adc_sample();

private:
    void sendWave(float max,float min,float data1,float data2, float data3,float data4);

public:
    TIM_HandleTypeDef *htim;
    ADC_HandleTypeDef *hadc;
    uint32_t TIM_CHANNEL_U,TIM_CHANNEL_V,TIM_CHANNEL_W,TIM_CHANNEL_InJ;

    AS5047_Driver *as5047;

    Aurora_PID I_pid_q,I_pid_d;
    Aurora_PID Speed_pid;   //速度PID
    Aurora_PID Position_pid;//位置PID

public:
    float target_Iq; //目标Iq
    float target_Id; //目标Id

public:
    uint8_t Pole_Pair; //电机的极对数
    float Ualpha,Ubeta;
    float Ua,Ub,Uc;
    float Ud,Uq;
    float Id,Iq;
    float Ia,Ib,Ic;
    float Ialpha,Ibeta;
    float Ts;  //控制周期
    float mechanical_angle;  //机械角度
    float mechanical_offset; //机械角度偏移值
    float sinVal,cosVal;     //角度的sin cos值
    float speed;
    float angle;
    float pos_gap;
    float position;
private:
  //  float angle;
    float Udc; //母线电压V

    //adc 相关参数

    uint8_t  motor_rotate_direct;
private:
    float ADC_multiple; //ADC放大倍数
    float Sample_R; //采用电阻

    float ADC_Uu_offset; //ADC U相电压零点偏移
    float ADC_Uv_offset; //ADC V相电压零点偏移
    float ADC_Uw_offset; //ADC W相电压零点偏移


private:
    Aurora_SEND_FOC_TYPE _wave_type;

private:
    float _tx_max,_tx_min;
    float _Ix_max,_Ix_min;
    float _Iqd_max,_Iqd_min;
    float _Ialphabeta_max,_Ialphabeta_min;
};
#endif //AURORA_ST_AURORA_FOC_H
